using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorApp.Data;
using Blazor.Polyfill.Server;

namespace BlazorApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
            //注册BlazorPolyfill

            services.AddBlazorPolyfill();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            //启用BlazorPolyfill,一定要在静态文件中间件之前启用,启用BlazorPolyfill中间件对blazor.polyfill.min.js和blazor.server.js文件请求,进行了拦截处理
            app.UseBlazorPolyfill();

            //
            //ForceES5Fallback默认为false,会自动判断浏览器是否支持es 5,如果该浏览器不支持的es5,则需要加载blazor.polyfill.min.js文件,如果该浏览器支持es 5则把blazor.polyfill.min.js请求返回返回_fakeBlazorPolyfill={}
            //app.UseBlazorPolyfill((options) =>
            //{
            //    options.ForceES5Fallback = true;
            //});

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}
